//package com.woniuxy.interceptor;
//
//import com.alibaba.fastjson2.JSON;
//import com.alibaba.fastjson2.JSONObject;
//import com.woniuxy.anno.BloomFilterAnnotation;
//import com.woniuxy.util.ResponseData;
//import com.woniuxy.util.ResponseEnum;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.util.ContentCachingRequestWrapper;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.lang.reflect.Method;
//
//
///**
// * @program: tsm
// * @description: 布隆过滤器的拦截器
// * @create: 2024-10-16 20:23
// **/
//@Component
//public class BloomInterceptor implements HandlerInterceptor {
//
////    @Autowired
////    private BloomFilterService bloomFilterService;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println("now join in BloomInterceptor");
//
//        // 判断请求类型，确保包装的请求体能被读取
//        if (handler instanceof HandlerMethod) {
//            // 使用 ContentCachingRequestWrapper 包装请求
//            ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
//
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            Method method = handlerMethod.getMethod();
//            BloomFilterAnnotation bloomFilterCheck = method.getAnnotation(BloomFilterAnnotation.class);
//
//            // 如果方法上有 @BloomFilterAnnotation 注解
//            if (bloomFilterCheck != null) {
//                // 读取缓存的请求体
//                String body = getRequestBody(request);
//
//                if (body != null && !body.isEmpty()) {
//                    JSONObject jsonObject = JSON.parseObject(body);
//                    String id = jsonObject.getString(bloomFilterCheck.value());
//
//                    if (id != null && !id.isEmpty()) {
//                        // 检查布隆过滤器
////                        boolean passFilter = bloomFilterService.checkInBloomFilter(id);
//
//                        if (false) {
//                            // 如果没有通过布隆过滤器校验，拦截请求
////                            bloomFilterService.addToBloomFilter(id); // 记录该 id
//                            response.setContentType("application/json;charset=utf-8");
//                            response.getWriter().write(JSON.toJSONString(new ResponseData<>().fail(ResponseEnum.NO_PERMISSION)));
//                            return false; // 终止请求，返回无权限
//                        }
//                    }
//                }
//            }
//        }
//
//        return true; // 继续执行后续流程
//    }
//
//    private String getRequestBody(HttpServletRequest request) throws IOException {
//        BufferedReader rd = request.getReader();
//        String line = null;
//        String message = new String();
//        final StringBuffer buffer = new StringBuffer(2048);
//        while ((line = rd.readLine()) != null) {
//            // buffer.append(line);
//            message += line;
//        }
//        return message;
//
//
//
//
////        if (rd!=null ) {
////            return new String(rd, request.getCharacterEncoding());
//////            return queryString;
////        }
////        return null; // 如果没有请求体，返回 null
//    }
//}